package com.sanpu.kxe.business.trade.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.sanpu.kxe.business.trade.bean.BillTypeOutput;
import com.sanpu.kxe.business.trade.bean.FastPayBannerOutput;
import com.sanpu.kxe.business.trade.bean.MyDividendOutput;
import com.sanpu.kxe.business.trade.bean.MyRebTotalOutput;
import com.sanpu.kxe.business.trade.bean.PayCollectionOutput;
import com.sanpu.kxe.business.trade.bean.PayOrderOutput;
import com.sanpu.kxe.business.trade.bean.PayStatusOutput;
import com.sanpu.kxe.business.trade.bean.PaymentOutput;
import com.sanpu.kxe.business.trade.bean.RebTotalOutput;
import com.sanpu.kxe.business.trade.bean.RechargeDetailOutput;
import com.sanpu.kxe.business.trade.bean.SupportBankOutput;
import com.sanpu.kxe.business.trade.bean.TransactionOutput;
import com.sanpu.kxe.business.trade.bean.WithdrawDictinaryOutput;
import com.sanpu.kxe.jump.eventbus.FilterEvent;
import com.sanpu.kxe.rxservice.DataUtils;
import com.sanpu.kxe.rxservice.client.TradeClient;

import java.math.BigDecimal;
import java.util.List;

import rx.Observable;

/**
 * Description：交易model
 * Created by 薛勇军 on 2018/8/8.
 */

public class TradeModel {

    private static volatile TradeModel mInstance;

    private TradeModel() {
        if (mInstance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static TradeModel getInstance() {
        TradeModel result = mInstance;
        if (result == null) {
            synchronized (TradeModel.class) {
                result = mInstance;
                if (result == null) {
                    mInstance = result = new TradeModel();
                }
            }
        }
        return result;
    }

    /**
     * 我的返佣列表
     */
    public Observable<List<MyRebTotalOutput>> myHstRebateList(int nowPage, String rebateLevel) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", nowPage);
        reqData.put("rebateLevel", rebateLevel);
        return TradeClient.getInstance().myHstRebateList(reqData);
    }

    /**
     * 我的返佣列表
     */
    public Observable<List<RebTotalOutput>> getMyHstRebateList(int nowPage, FilterEvent filterEvent) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", nowPage);
        if (filterEvent != null) {
            reqData.put("startTime", filterEvent.getStartTime());
            reqData.put("endTime", filterEvent.getEndTime());
            reqData.put("rebateType", filterEvent.getTransactionType());
            reqData.put("month", filterEvent.getMonth());
        }
        return TradeClient.getInstance().getMyHstRebateList(reqData);
    }

    /**
     * 获取返佣类型分类
     */
    public Observable<List<BillTypeOutput>> getRebateType() {
        return TradeClient.getInstance().getRebateType();
    }

    /**
     * 提现
     */
    public Observable<String> userWithdraw(String withdrawAmt, String payPassword, String accountName, String bankId) {
        JSONObject reqData = new JSONObject();
        reqData.put("amount", withdrawAmt);
        reqData.put("payPwd", DataUtils.getMD5Str(payPassword + accountName));
        reqData.put("stlBankId", bankId);
        return TradeClient.getInstance().userWithdraw(reqData);
    }

    /**
     * 获取当前的提现限额
     */
    public Observable<WithdrawDictinaryOutput> withdrawDictinary() {
        return TradeClient.getInstance().withdrawDictinary();
    }

    /**
     * 账单列表
     */
    public Observable<List<TransactionOutput>> getBillList(int page, FilterEvent filterEvent) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", page);
        if (filterEvent != null) {
            reqData.put("startTime", filterEvent.getStartTime());
            reqData.put("endTime", filterEvent.getEndTime());
            reqData.put("transactionType", filterEvent.getTransactionType());
            reqData.put("startMoney", filterEvent.getStartMoney());
            reqData.put("endMoney", filterEvent.getEndMoney());
            reqData.put("month", filterEvent.getMonth());
        }
        return TradeClient.getInstance().getBillList(reqData);
    }

    /**
     * 提现账单列表
     */
    public Observable<List<TransactionOutput>> withDrawBillList(int page, String startTime, String endTime) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", page);
        if (!TextUtils.isEmpty(startTime))
            reqData.put("startTime", startTime);
        if (!TextUtils.isEmpty(endTime))
            reqData.put("endTime", endTime);
        return TradeClient.getInstance().withDrawBillList(reqData);
    }

    /**
     * 获取账单分类
     */
    public Observable<List<BillTypeOutput>> getBillType() {
        return TradeClient.getInstance().getBillType();
    }

    /**
     * 收款渠道列表
     *
     * @param receivablesType PERSON_COLLECTION：个人收款
     *                        MERCHANT_COLLECTION：商家收款
     *                        VIP_RECHARGE：会员充值
     *                        SHOP：商城
     *                        WITHDRAW：提现
     *                        REPAY_CONSUME：代还消费
     */
    public Observable<List<PayCollectionOutput>> appCnlConfList(String receivablesType) {
        JSONObject reqData = new JSONObject();
        reqData.put("receivablesType", receivablesType);
        return TradeClient.getInstance().appCnlConfList(reqData);
    }

    /**
     * 还款渠道列表
     */
    public Observable<List<PayCollectionOutput>> appCnlConfList(String receivablesType, String mercCardId, String version) {
        JSONObject reqData = new JSONObject();
        reqData.put("receivablesType", receivablesType);
        reqData.put("mercCardId", mercCardId);
        return TradeClient.getInstance().appCnlConfList(reqData, version);
    }

    /**
     * 获取APP通道配置银行限额列表
     *
     * @param appCnlConfId 通道配置Id
     */
    public Observable<List<SupportBankOutput>> findAppCnlConfBankSupportDetailList(String appCnlConfId) {
        if (!TextUtils.isEmpty(appCnlConfId)) {
            JSONObject reqData = new JSONObject();
            reqData.put("appCnlConfId", appCnlConfId);
            return TradeClient.getInstance().findAppCnlConfBankSupportDetailList(reqData);
        } else {
            return TradeClient.getInstance().appBaseBank();
        }
    }

    /**
     * 获取APP通道或添加信用卡可支持的银行卡
     *
     * @param appCnlConfId 通道配置Id
     */
    public Observable<List<SupportBankOutput>> appBaseBank(String appCnlConfId) {
        if (!TextUtils.isEmpty(appCnlConfId)) {
            JSONObject reqData = new JSONObject();
            reqData.put("appCnlConfId", appCnlConfId);
            return TradeClient.getInstance().findAppCnlConfBankSupportDetailList(reqData);
        } else {
            return TradeClient.getInstance().appBaseBank();
        }
    }

    /**
     * 生成支付订单
     *
     * @param payAmount 支付金额
     * @param payType   支付渠道类型  如QQ、微信
     * @param constype  支付类型 如花费、流量（充值会员 recharge  扫码  consume）
     * @param pkgId     套餐id
     * @param phoneNo   手机号码
     * @return
     */
    public Observable<PaymentOutput> userPayment(String payAmount, String payType, String constype,
                                                 String pkgId, String phoneNo) {
        JSONObject reqData = new JSONObject();
        reqData.put("payAmount", payAmount);
        reqData.put("payType", payType);
        reqData.put("constype", constype);
        if (!TextUtils.isEmpty(pkgId)) {
            reqData.put("pkgId", pkgId);
        }
        if (!TextUtils.isEmpty(phoneNo)) {
            reqData.put("phoneNo", phoneNo);
        }
        return TradeClient.getInstance().userPayment(reqData);
    }

    /**
     * 快捷native收款广告
     *
     * @return
     */
    public Observable<List<FastPayBannerOutput>> lvlFee() {
        return TradeClient.getInstance().lvlFee();
    }

    /**
     * 获取归属地和话费流量充值套餐信息（首页=>话费流量）
     *
     * @return
     */
    public Observable<RechargeDetailOutput> getRechargeDetail(String phoneNumber) {
        JSONObject reqData = new JSONObject();
        reqData.put("phoneNumber", phoneNumber);
        return TradeClient.getInstance().getRechargeDetail(reqData);
    }

    /**
     * 话费充值（首页=>话费流量=>立即充值）
     *
     * @return
     */
    public Observable<String> recharge(
            String phoneNumber, String buyType, String payType, int pkgId, String payPassword, String accountName) {
        JSONObject reqData = new JSONObject();
        reqData.put("phoneNumber", phoneNumber);
        reqData.put("buyType", buyType);
        reqData.put("payType", payType);
        reqData.put("pkgId", pkgId);
        reqData.put("payPassword", DataUtils.getMD5Str(payPassword + accountName));
        return TradeClient.getInstance().recharge(reqData);
    }

    /**
     * 充值会员统一下单
     *
     * @return
     */
    public Observable<PayOrderOutput> payOrder(String amount, String appCnlConfId, String vipOpenId) {
        JSONObject reqData = new JSONObject();
        reqData.put("amount", amount);
        reqData.put("appCnlConfId", appCnlConfId);
        reqData.put("vipOpenId", vipOpenId);
        return TradeClient.getInstance().payOrder(reqData);
    }

    /**
     * 商城统一下单
     *
     * @return
     */
    public Observable<PayOrderOutput> payOrder(BigDecimal amount, String appCnlConfId, String outTradeNo) {
        JSONObject reqData = new JSONObject();
        reqData.put("amount", amount);
        reqData.put("appCnlConfId", appCnlConfId);
        reqData.put("outTradeNo", outTradeNo);
        return TradeClient.getInstance().payOrder(reqData);
    }

    /**
     * 获取支付状态
     *
     * @return
     */
    public Observable<PayStatusOutput> findTranPayStatus(String orderNo) {
        JSONObject reqData = new JSONObject();
        reqData.put("orderNo", orderNo);
        return TradeClient.getInstance().findTranPayStatus(reqData);
    }

    /**
     * 我的分红
     *
     * @return
     */
    public Observable<MyDividendOutput> myDividend() {
        return TradeClient.getInstance().myDividend();
    }
}
